package cn.app.web.controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpSession;

import org.apache.catalina.connector.Request;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import cn.app.entity.Commodity;
import cn.app.service.MerchandiseDisplay;

@Controller
@RequestMapping("/DevAppCommodityPurchase")
public class DevAppCommodityPurchase {

	@Autowired
	private MerchandiseDisplay merchandiseDisplay;
	
	@RequestMapping("/CommodityHomepage")
	public String commodityHomepage(Model model,HttpSession session)
	{
		String username = (String)session.getAttribute("devcode");
		List<Commodity> listCommodity = merchandiseDisplay.query();
	
		model.addAttribute("listCommodity", listCommodity);
		model.addAttribute("username", username);
		return "dev/devAppCommodity";
	}
	@RequestMapping("/addHomepage")
	public String addHomepage()
	{
		
		return "dev/devAppCommodityAdd";
	}
	
	@RequestMapping("/addCommodityHomepage")
	public String addCommodityHomepage(Model model,Commodity commodity,MultipartFile picFile) throws Exception
	{
		if(picFile.getSize() > 0){
			//获得原名字
			String originalName = picFile.getOriginalFilename();
			//校验图片必须是.jpg/.png/.jpeg
			String typeName = originalName.substring(originalName.lastIndexOf("."));
			//判断
			if(!(".jpg".equals(typeName) ||".png".equals(typeName) ||".jpeg".equals(typeName))){
				//图片格式不正确
				model.addAttribute("picInfo", "图片格式不正确");
				return "forward:/devApp/disAppInfoAdd";
			}
			//保存图片
			//设置新名字
			String newName = UUID.randomUUID().toString() + typeName;
			//保存/转移
			picFile.transferTo(new File("E:\\pic\\" + newName));
			
			commodity.setPic("http://localhost:8080/pic/" + newName);
			commodity.setLogolocpath("E:\\pic\\" + newName);
			
			
		}
		
		int i = merchandiseDisplay.addGoods(commodity);
	
		
		
		
		return "redirect:/DevAppCommodityPurchase/CommodityHomepage";
	}
	
	@RequestMapping("/shoppingCart")
	public String shoppingCart( Commodity commodity,HttpSession session)
	{
	
		
		String username = (String)session.getAttribute("devcode");
		System.out.println(username);
		Commodity listCommodity = merchandiseDisplay.queryId(commodity);
		System.out.println(listCommodity);
		
		listCommodity.setUsername(username);
		merchandiseDisplay.addToShoppingCart(listCommodity);
		
		return "redirect:/DevAppCommodityPurchase/CommodityHomepage";
		
	}
	@RequestMapping("/shoppingCart2")
	public String shoppingCart2( HttpSession session,Model model)
	{
		
		
		String username = (String)session.getAttribute("devcode");
		
	     List<Commodity> Commodityname =	merchandiseDisplay.ShoppingCartandName(username);
	     
	     model.addAttribute("listCommodity", Commodityname);
		return "dev/devAppCommodity";
		
	}
}
